package com.byedu.jupiter.util.parsers.library;

import com.byedu.jupiter.domain.course.AnswerDO;
import com.byedu.jupiter.domain.course.QuestionDO;
import com.byedu.jupiter.util.ExcelUtil;
import com.byedu.jupiter.util.enums.JupiterConstants;
import com.byedu.jupiter.util.enums.SubOptionType;
import com.byedu.jupiter.util.parsers.AbstractFileParser;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by jason on 2017/6/4.
 */
public class LibraryExcelParser extends AbstractFileParser {

    private static final Logger logger = LoggerFactory.getLogger(LibraryExcelParser.class);

    public LibraryExcelParser() {
    }

    @Override
    public Map<String, Object> parseFile(File file, int answerColumnSize, boolean selectionFlag) throws Exception {
        InputStream is = new FileInputStream(file);
        Workbook workbook = ExcelUtil.create(is);
        if (workbook == null) {
            logger.error("请上传xls或者xlsx格式的文件！");
            throw new Exception("请上传xls或者xlsx格式的文件！");
        }

        Sheet sheet = workbook.getSheetAt(0);
        if (sheet == null) {
            logger.error("获取Excel表Sheet错误！");
            throw new Exception("获取Excel表Sheet错误！！");
        }

        if (sheet.getPhysicalNumberOfRows() <= 1) {
            logger.error("文件内容为空！");
            throw new Exception("文件内容为空！");
        }

        //解析数据
        Map<String, Object> map = new HashMap<String, Object>();
        List<AnswerDO> answerDOList = new ArrayList<AnswerDO>();
        List<QuestionDO> questionDOList = new ArrayList<QuestionDO>();
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            QuestionDO questionDO = new QuestionDO();
            Row row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            //序号
            String serialno = ExcelUtil.getCellValue(row.getCell(0));
            if (StringUtils.isNotEmpty(serialno)) {
                Integer id = Integer.parseInt(serialno);

            } else {
                logger.error("获取的序号为空！");
                throw new Exception("获取的序号为空！");
            }

            //题目
            String lotteryTypeName = ExcelUtil.getCellValue(row.getCell(1));
            if (StringUtils.isNotEmpty(lotteryTypeName)) {

            } else {
                logger.error("获取题目错误！");
                throw new Exception("获取题目错误！");
            }

            //获取选项+标准答案
            int index = 2;
            List<AnswerDO> answerDOs = getAnswerDOList(row, index, answerColumnSize, selectionFlag);
            answerDOList.addAll(answerDOs);
            index = index + answerColumnSize;

            //获取试题解析
            String analysis = ExcelUtil.getCellValue(sheet.getRow(i).getCell(index));
            if (StringUtils.isNotEmpty(analysis)) {

            } else {
                logger.error("获取正确答案错误！");
                throw new Exception("获取正确答案错误！");
            }

            questionDOList.add(questionDO);
        }

        map.put(JupiterConstants.QUESTION_LIST, questionDOList);
        map.put(JupiterConstants.ANSWER_LIST, answerDOList);

        return map;
    }

    /**
     * 获取选项
     *
     * @param row
     * @param startIndex
     * @param answerColumnSize
     * @return
     * @throws Exception
     */
    public List<AnswerDO> getAnswerDOList(Row row, int startIndex, int answerColumnSize, boolean selectionFlag) {
        List<AnswerDO> answerDOs = new ArrayList<AnswerDO>();
        Set<String> correctOptions = getCorrectOptions(row, (startIndex + answerColumnSize));
        if (selectionFlag) {// 选择题
            int optionIndex = 0;
            do {
                String optionConext = ExcelUtil.getCellValue(row.getCell(startIndex));
                if (StringUtils.isNotEmpty(optionConext)) {
                    AnswerDO answerDO = new AnswerDO();
                    String subOption = SubOptionType.getValue(optionIndex);
                    if (correctOptions.contains(subOption)) {
                        //该选项是标准答案
                    }
                    optionIndex++;
                    startIndex++;
                    answerColumnSize--;
                    answerDOs.add(answerDO);
                } else {
                    break;
                }
            } while (answerColumnSize > 1);
        } else {//非选择题
            String optionConext = ExcelUtil.getCellValue(row.getCell(startIndex));
            if (StringUtils.isNotEmpty(optionConext)) {
                AnswerDO answerDO = new AnswerDO();
                answerDOs.add(answerDO);
            }
        }

        return answerDOs;
    }

    public Set<String> getCorrectOptions(Row row, int index) {
        //正确答案
        String correctContext = ExcelUtil.getCellValue(row.getCell(index));
        Set<String> options = new HashSet<String>();
        if (StringUtils.isNotEmpty(correctContext)) {
            for (int i = 0; i < correctContext.length(); i++) {
                char option = correctContext.charAt(i);
                options.add(String.valueOf(option));
            }
        }

        return options;
    }

}
